﻿using System;
using System.Deployment.Application;
using System.Threading.Tasks;
using CloudShot.Interfaces;
using CloudShot.Utils;

namespace CloudShot.Services
{
  public class ApplicationUpdaterService : IApplicationUpdaterService
  {
    public async Task<bool> IsUpdateAvailable()
    {
      //http://msdn.microsoft.com/en-us/library/ms404263.aspx
      if (!ApplicationDeployment.IsNetworkDeployed)
        return false;

      try
      {
        var applicationDeployment = ApplicationDeployment.CurrentDeployment;
        return await Task.Factory.StartNew(() => applicationDeployment.CheckForUpdate(false));
      }
      catch (Exception err)
      {
        ErrorsHelper.TraceError(err);
      }

      return false;
    }

    public async Task<bool> InstallUpdate()
    {
      try
      {
        var applicationDeployment = ApplicationDeployment.CurrentDeployment;
        return await Task.Factory.StartNew(() => applicationDeployment.Update());
      }
      catch (Exception err)
      {
        ErrorsHelper.TraceError(err);
      }

      return false;
    }
  }
}